// Copyright (c) 2007, Brian Duff

// See LICENSE for full license details.

package org.dubh.jdant;

import java.util.Collection;


/**
 * String utilities.
 *
 * @author brian.duff@dubh.org
 */
public final class Strings
{
  private Strings()
  {
  }
  
  /**
   * Converts a collection of strings into a single string, with each item 
   * separated by the specified separator character.
   * 
   * @param strings a collection of strings.
   * @param separator a character to separate each item in the return string.
   * @return a string containing all the items in <tt>strings</tt>, separated
   *    by <tt>separator</tt>
   *    
   * @throws NullPointerException if <tt>strings</tt> is <tt>null</tt>.
   */
  public static String collectionToString( Collection<String> strings, 
    char separator )
  {
    if ( strings == null ) throw new NullPointerException( "strings is null" );
    
    StringBuilder sb = new StringBuilder();
    for ( String string: strings )
    {
      sb.append( string );
      sb.append( separator );
    }
    removeLastCharacter( sb );
    
    return sb.toString();    
  }
  
  /**
   * Removes the last character from a {@link StringBuilder} unless it is
   * empty.
   * 
   * @param builder a builder to remove the last character from.
   * @throws NullPointerException if <tt>builder</tt> is <tt>null</tt>.
   */
  public static void removeLastCharacter( StringBuilder builder )
  {
    if ( builder.length() > 0 ) builder.setLength( builder.length() - 1 );
  }
}
